<?php
/*
* Copyright 2017 Baidu, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

namespace BaiduBce\Services\Eip;

use BaiduBce\Auth\BceV1Signer;
use BaiduBce\BceBaseClient;
use BaiduBce\Http\BceHttpClient;
use BaiduBce\Http\HttpHeaders;
use BaiduBce\Http\HttpMethod;
use BaiduBce\Http\HttpContentTypes;
use BaiduBce\Services\Eip\model\Billing;

/**
 * This module provides a client class for EIP.
 */
class EipClient extends BceBaseClient
{

    private $signer;
    private $httpClient;
    private $prefix = '/v1';

    /**
     * The BccClient constructor.
     *
     * @param array $config The client configuration
     */
    function __construct(array $config)
    {
        parent::__construct($config, 'eip');
        $this->signer = new BceV1Signer();
        $this->httpClient = new BceHttpClient();
    }

    /**
     * Create an eip with the specified options.
     *
     * @param int $bandwidthInMbps
     *          specify the bandwidth in Mbps
     *
     * @param string $name
     *          name of eip. The optional parameter
     *
     * @param Billing $billing
     *          billing information. The optional parameter, default paymentTiming is Postpaid
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function createEip($bandwidthInMbps, $name=null, $billing=null, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        $body = array();
        $body['name'] = $name;
        $body['bandwidthInMbps'] = $bandwidthInMbps;
        if (empty($billing)) {
            $billing = $this->generateDefaultBilling();
        }
        $body['billing'] = $this->objectToArray($billing);
        $params = array();
        if (empty($clientToken)) {
            $params['clientToken'] = $this->generateClientToken();
        } else {
            $params['clientToken'] = $clientToken;
        }

        return $this->sendRequest(
            HttpMethod::POST,
            array(
                'config' => $config,
                'params' => $params,
                'body' => json_encode($body),
            ),
            '/eip'
        );
    }

    /**
     * Resizing eip
     *
     * @param string $eip
     *          eip address to be resized
     *
     * @param int $newBandwidthInMbps
     *          specify new bandwidth in Mbps for eip
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function resizeEip($eip, $newBandwidthInMbps, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        $body = array();
        $body['newBandwidthInMbps'] = $newBandwidthInMbps;
        $params = array();
        $params['resize'] = null;
        if (empty($clientToken)) {
            $params['clientToken'] = $this->generateClientToken();
        } else {
            $params['clientToken'] = $clientToken;
        }

        return $this->sendRequest(
            HttpMethod::PUT,
            array(
                'config' => $config,
                'params' => $params,
                'body' => json_encode($body),
            ),
            '/eip/' . $eip
        );
    }

    /**
     * PurchaseReserved eip with fixed duration,only Prepaid eip can do this
     *
     * @param string $eip
     *          eip address to be renewed
     *
     * @param Billing $billing
     *          billing information. The optional parameter, default reservationLength is 1
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function purchaseReservedEip($eip, $billing=null, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        $body = array();
        if (empty($billing)) {
            $reservation = array();
            $reservation['reservationTimeUnit'] = 'Month';
            $reservation['reservationLength'] = 1;
            $billingBody = array();
            $billingBody['reservation'] = $reservation;
            $body['billing'] = $billingBody;
        } else {
            $reservation = array();
            $reservation['reservationTimeUnit'] = $billing->reservation->reservationTimeUnit;
            $reservation['reservationLength'] = $billing->reservation->reservationLength;
            $billingBody = array();
            $billingBody['reservation'] = $reservation;
            $body['billing'] = $billingBody;
        }
        $params = array();
        if (empty($clientToken)) {
            $clientToken = $this->generateClientToken();
        }
        $params['purchaseReserved'] = null;
        $params['clientToken'] = $clientToken;

        return $this->sendRequest(
            HttpMethod::PUT,
            array(
                'config' => $config,
                'params' => $params,
                'body' => json_encode($body),
            ),
            '/eip/' . $eip
        );
    }

    /**
     * bind the eip to a specified instanceId and instanceType
     *
     * @param string $eip
     *          eip address to be bound
     *
     * @param string $instanceType
     *          type of instance to be bound(BCC BLB et.)
     *
     * @param string $instanceId
     *          id of instance to be bound
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function bindEip($eip, $instanceType, $instanceId, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        $body = array();
        $body['instanceType'] = $instanceType;
        $body['instanceId'] = $instanceId;
        $params = array();
        if (empty($clientToken)) {
            $clientToken = $this->generateClientToken();
        }
        $params['bind'] = '';
        $params['clientToken'] = $clientToken;

        return $this->sendRequest(
            HttpMethod::PUT,
            array(
                'config' => $config,
                'params' => $params,
                'body' => json_encode($body),
            ),
            '/eip/' . $eip
        );
    }

    /**
     * unbind the eip from a specified instance
     *
     * @param string $eip
     *          eip address to be unbound
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function unbindEip($eip, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        if (empty($clientToken)) {
            $clientToken = $this->generateClientToken();
        }
        $params = array();
        $params['unbind'] = '';
        $params['clientToken'] = $clientToken;

        return $this->sendRequest(
            HttpMethod::PUT,
            array(
                'config' => $config,
                'params' => $params,
            ),
            '/eip/' . $eip
        );
    }

    /**
     * release the eip(delete operation)
     * Only the Postpaid instance or Prepaid which is expired can be released.
     * if the eip has been bound, must unbind before releasing.
     *
     * @param string $eip
     *          eip address to be released
     *
     * @param string $clientToken
     *          if the clientToken is not specified by the user, a random String
     *          generated by default algorithm will be used.
     *
     * @param array $options
     *          The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function releaseEip($eip, $clientToken=null, $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        if (empty($clientToken)) {
            $clientToken = $this->generateClientToken();
        }
        $params = array();
        $params['clientToken'] = $clientToken;

        return $this->sendRequest(
            HttpMethod::DELETE,
            array(
                'config' => $config,
                'params' => $params,
            ),
            '/eip/' . $eip
        );
    }

    /**
     * get a list of eip owned by the authenticated user and specified conditions.
     * we can Also get a single eip function  through this interface by eip condition
     *
     * @param string $eip
     *          eip address condition
     *
     * @param string $instanceType
     *          bound instance type condition
     *
     * @param string $instanceId
     *          bound instance id condition
     *          if query by the instanceId or instanceType condition,
     *          must provides both of them at the same time
     *
     * @param string $marker
     *          The optional parameter marker specified in the original request to specify
     *          where in the results to begin listing.
     *
     * @param int $maxKeys
     *          The optional parameter to specifies the max number of list result to return.
     *          The default value is 1000.
     *
     * @param array $options
     *           The optional bce configuration, which will overwrite the
     *          default configuration that was passed while creating EipClient instance.
     *
     * @return mixed
     */
    public function listEips($eip=null, $instanceType=null, $instanceId=null, $marker=null, $maxKeys=1000,
                                $options = array())
    {
        list($config) = $this->parseOptions($options, 'config');
        $params = array();
        if ($eip !== null) {
            $params['eip'] = $eip;
        }
        if ($instanceType !== null) {
            $params['instanceType'] = $instanceType;
        }
        if ($instanceId !== null) {
            $params['instanceId'] = $instanceId;
        }
        if ($marker !== null) {
            $params['marker'] = $marker;
        }
        if ($maxKeys !== null) {
            $params['maxKeys'] = $maxKeys;
        }

        return $this->sendRequest(
            HttpMethod::GET,
            array(
                'config' => $config,
                'params' => $params,
            ),
            '/eip'
        );
    }

    /**
     * convert object to array
     *
     * @param object $obj
     *
     * @return array
     */
    function objectToArray($obj)
    {
        if (is_array($obj)) {
            return $obj;
        }
        $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
        $arr = array();
        foreach ($_arr as $key => $value) {
            $value = (is_array($value)) || is_object($value) ? $this->objectToArray($value) : $value;
            $arr[$key] = $value;
        }
        return $arr;
    }

    /**
     * Create HttpClient and send request
     *
     * @param string $httpMethod
     *          The Http request method
     *
     * @param array $varArgs
     *          The extra arguments
     *
     * @param string $requestPath
     *          The Http request uri
     *
     * @return mixed The Http response and headers.
     */
    private function sendRequest($httpMethod, array $varArgs, $requestPath = '/')
    {
        $defaultArgs = array(
            'config' => array(),
            'body' => null,
            'headers' => array(),
            'params' => array(),
        );

        $args = array_merge($defaultArgs, $varArgs);
        if (empty($args['config'])) {
            $config = $this->config;
        } else {
            $config = array_merge(
                array(),
                $this->config,
                $args['config']
            );
        }
        if (!isset($args['headers'][HttpHeaders::CONTENT_TYPE])) {
            $args['headers'][HttpHeaders::CONTENT_TYPE] = HttpContentTypes::JSON;
        }
        $path = $this->prefix . $requestPath;
        $response = $this->httpClient->sendRequest(
            $config,
            $httpMethod,
            $path,
            $args['body'],
            $args['headers'],
            $args['params'],
            $this->signer
        );

        $result = $this->parseJsonResult($response['body']);

        return $result;
    }

    /**
     * The method to generate a default Billing which is Postpaid.
     *
     * @return Billing object with Postpaid PaymentTiming.
     */
    private function generateDefaultBilling() {
        return new Billing('Postpaid', 'ByBandwidth');
    }

    /**
     * The default method to generate the random String for clientToken if the optional parameter clientToken
     * is not specified by the user.
     *
     * The default algorithm is Mersenne Twister to generate a random UUID,
     * @return String An random String generated by Mersenne Twister.
     */
    public static function generateClientToken()
    {
        $uuid = md5(uniqid(mt_rand(), true));
        return $uuid;
    }
}